/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.matching;

import cgl.narada.util.ByteUtilities;

/**
 * The EventId is an ID which is unique in space and time. Using the event ID
 * we can decipher if the event was an earlier event or a later event. This 
 * is what provides us with information pertaining to duplicate detection.
 *
 * @author Shrideep Pallickara
 * $Revision$
 * $Date$
 */

public class EventId {
  /** Every issuing client is assigned a unique ID. This reduces the uniqueness
      problem to a point in space */
  private int clientID;
  /** The time in milli-seconds  */ 
  private long timeStamp;
  /** We use this so that our issue rate isn't determined by the granularity
      of the clock in the underlying system. The sequence numbers start from
      zero and continue till it reaches Short.MAX_VALUE
  */
  private short seqNum;
  /** This is something which needs to be advanced by the client, for every 
      re-join after a disconnected mode operation */
  private short incarnationNumber;
  
 
  /** This constructor provides for the unmarshalling of the eventID bytes
      that are received over a link
      @param idBytes A byte stream representation of the event ID.
  */
  public EventId(byte[] idBytes) {
    int offset=0;
    /** Marshall the clientID */
    byte[] clientBytes = new byte[4];
    System.arraycopy(idBytes,offset, clientBytes,0,4);
    offset+=4;
    clientID = ByteUtilities.getInt(clientBytes);

    /** Marshall the timeStamp */
    byte[] timeStampBytes = new byte[8];
    System.arraycopy(idBytes,offset, timeStampBytes,0,8);
    offset+=8;
    timeStamp = ByteUtilities.getLong(timeStampBytes);

    /** Marshall the sequence Number */
    byte[] seqNumBytes = new byte[2];
    System.arraycopy(idBytes,offset, seqNumBytes,0,2);
    offset+=2;
    seqNum = ByteUtilities.getShort(seqNumBytes);

    /** Marshall the incarnation number */
    byte[] incarnationBytes = new byte[2];
    System.arraycopy(idBytes,offset, incarnationBytes,0,2);
    incarnationNumber = ByteUtilities.getShort(incarnationBytes);
    offset+=2;
  }
  
  /** Constructs a unique EventId based on the clientID, timeStamp,
      sequenceNumber and the incarnation associated with the publishing
      client.
      @param _clientID The clientID
      @param _timeStamp The timestamp associated with the ID.
      @param _seqNum The sequence number
      @param _incarnationNumber The incarnation associated with the publishing
      client.
  */
  public EventId(int _clientID, long _timeStamp, 
		  short _seqNum, short _incarnationNumber) {
    clientID = _clientID;
    timeStamp = _timeStamp;
    seqNum    = _seqNum;
    incarnationNumber = _incarnationNumber;
  }
  
  /** Returns the client ID contained in the eventID 
      @return int clientID */
  public int getClientID() {
    return clientID;
  }
  
  /** This method returns the timeStamp that is associated with the eventID.
      @return long timeStamp  */
  public long getTimeStamp() {
    return timeStamp;
  }
  
  /** This method returns the sequence number associated with the eventID
      instance.
      @return short sequenceNumber */
  public short getSequenceNumber() {
    return seqNum;
  }
  
  /** This returns the incarnation number associated with the publishing 
      client. A roam also constitutes as a new incarnation, and increments the
      incarnation sequence associated with the client.
      @return short incarnationNumber */
  public short getIncarnation() {
    return incarnationNumber;
  }
  
  /* Returns a string representation of the EventId object.
     @return String stringRepOfObject */
  public String toString() {
    String stringRep = "Event ID:: clientID -> " + clientID + 
      "  timeStamp -> " + timeStamp + " seqNum -> " +
      seqNum + " incarnationNum ->" + incarnationNumber;
    return stringRep;    
  }
  
  
  /** This method provides a marshalling of the eventID bytes. 
      @return Byte stream representation of the eventID.
  */
  public byte[] getBytes() {
    byte[] idBytes = new byte[4 /* Client-ID */ + 8 /* Time Stamp */ +
			     2 /*Sequence Num*/ + 2 /* Incarnation Num */];
    int offset=0;
    byte[] clientBytes = ByteUtilities.getBytes(clientID);
    byte[] timeStampBytes = ByteUtilities.getBytes(timeStamp);
    byte[] seqNumBytes = ByteUtilities.getBytes(seqNum);
    byte[] incarnationBytes = ByteUtilities.getBytes(incarnationNumber);
    
    /** Marshall the client ID */
    System.arraycopy(clientBytes,0, idBytes, offset, 4);
    offset+=4;
    /** Marshall the timeStamp*/
    System.arraycopy(timeStampBytes,0, idBytes, offset, 8);
    offset+=8;
    /** Marshall the sequenceNum */
    System.arraycopy(seqNumBytes,0, idBytes, offset, 2);
    offset+=2;
     /** Marshall the Incarnation */
    System.arraycopy(incarnationBytes,0, idBytes, offset, 2);
    offset+=2; 
    return idBytes;  
    
  }

  public static void main(String[] args) {
    EventId eventId =  new EventId(123, (long)345, (short)0, (short)324);
    System.out.println(new EventId(eventId.getBytes()));
  }
}



